package com.hhh.demo.sx.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.demo.sx.bean.*;
import com.hhh.demo.sx.service.MenuService;
import com.hhh.demo.sx.service.ProductClassService;
import com.hhh.demo.sx.service.ProductService;
import com.hhh.demo.sx.service.RoleMenuService;
import com.hhh.demo.sx.util.ResultMap;
import com.hhh.demo.sx.util.ResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhh
 * @since 2022-01-04
 */
@Controller
@RequestMapping("/product_class")
public class ProductClassController {
    @Autowired
    private ProductClassService productClassService;


    /**
     * 打开菜单主页面
     */
    @RequestMapping("/index")
    public String openPage(){
        return "product_class";
    }

    /**
     * 查询菜单列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map<String, Object> getList(@RequestParam("page") int p,
                                       @RequestParam("limit") int limit,
                                       @RequestParam(value = "condition", required = false) String condition){

        return  productClassService.findAll(p,limit,condition);

    }
    /**
     * 新增/修改菜单
     */
    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    public ResultMap saveOrUpdate(ProductClass  cls){
        if(cls.getParentId()!=0){
            ProductClass p=productClassService.getById(cls.getParentId());
            cls.setParentName(p.getName());
        }
        boolean f=productClassService.saveOrUpdate(cls);
        if(f){
            return ResultMap.success();
        }else {
            return ResultMap.fail();
        }
    }
    /**
     * 查找菜单
     */
    @ResponseBody
    @RequestMapping("/getOne")
    public ResultMap getOne(int id){
        ProductClass p= productClassService.getById(id);
        return ResultMap.success(p);
    }
    /**
     * 删除菜单
     *  @Transactional 事务
     */
    @Transactional
    @ResponseBody
    @RequestMapping("/delete")
    public ResultMap delete(int id){
        boolean f=productClassService.removeById(id);
//        QueryWrapper<RoleMenu> wrapper =new QueryWrapper<>();
//        wrapper.eq("menu_id",id);
//        roleMenuService.remove(wrapper);
        if(f){
            return ResultMap.success();
        }else {
            return ResultMap.fail();
        }
    }
    /**
     * 获取所有菜单
     */
    @ResponseBody
    @RequestMapping("/getALL")
    public ResultMap delete(){
        return ResultMap.success(productClassService.list());
    }

//    /**
//     * 获取菜单的树结构
//     */
//    @ResponseBody
//    @RequestMapping("/getTree")
//    public ResultMap getTree(Integer id){
//
//        List<LayuiTree> tree=productClassService.selectMenuTree(id);
////        System.out.println(tree.get(1).getChecked());
//        return ResultMap.success(tree);
//    }



}

